package com.huike.vo;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 导出Excel
 */
public class ExportExcel<T> {
    /**
     * 文件导出方法
     *
     * @param resource     List<T> 具体要导出的数据集合
     * @param outputStream 输出流
     * @param headerStr    表头
     */
    public void exportExcel(List<T> resource, OutputStream outputStream, String[] headerStr) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        this.exportExcel(resource,outputStream,headerStr,null);
    }

    public void exportExcel(List<T> resource, OutputStream outputStream, String[] headerStr, List<String> allowFields) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //创建一个内存Excel对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个表格
        HSSFSheet sheet = workbook.createSheet("sheet1");
        //获取表头
        HSSFRow headerRow = sheet.createRow(0);
        //设置列宽
        for (int i = 0; i < headerStr.length; i++) {
            sheet.setColumnWidth(i, 5000);
        }
        //设置头单元格样式
        HSSFCellStyle hssfStyle = workbook.createCellStyle();
        hssfStyle.setAlignment(HorizontalAlignment.CENTER);//设置居中
        HSSFFont headerFont = workbook.createFont(); //设置字体
        headerFont.setFontName("黑体");
        hssfStyle.setFont(headerFont);
        for (int i = 0; i < headerStr.length; i++) {
            //创建一个单元格
            HSSFCell hssfCell = headerRow.createCell(i);
            hssfCell.setCellStyle(hssfStyle);
            hssfCell.setCellValue(headerStr[i]);
        }
        //设置表体样式
        HSSFCellStyle bodyStyle = workbook.createCellStyle();
        hssfStyle.setAlignment(HorizontalAlignment.CENTER);//设置居中
        HSSFFont bodyFont = workbook.createFont(); //设置字体
        headerFont.setFontName("宋体");
        hssfStyle.setFont(bodyFont);
        //创建数据
        for (int row = 0; row < resource.size(); row++) {
            //输出的行数据
            T t = resource.get(row);
            Field[] fields = t.getClass().getDeclaredFields();
            //创建行
            HSSFRow bodyRow = sheet.createRow(row + 1);
            for (int cell = 0; cell < fields.length; cell++) {
                HSSFCell bodyCell = bodyRow.createCell(cell);
                Field field = fields[cell];
                String fieldName = field.getName();
                if (allowFields != null && !allowFields.contains(fieldName)) {
                    continue;
                }
                String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                Class tCls = t.getClass();
                Method getMethod = tCls.getMethod(getMethodName, new Class[]{});//取出t中的get方法
                Object value = getMethod.invoke(t, new Object[]{});//使用getMethod将t中的值取出赋予value;
                String textValue = null;
                // 其它数据类型都当作字符串简单处理
                if (value != null && value != "") {
                    textValue = value.toString();
                }
                if (textValue != null) {
                    HSSFRichTextString richString = new HSSFRichTextString(textValue);
                    bodyCell.setCellStyle(bodyStyle);
                    bodyCell.setCellValue(richString);
                }
            }
        }
        //将内存创建的excel对象输出到文件中
        workbook.write(outputStream);
    }
}
